<!DOCTYPE html>
<html lang="zh-Hans">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0,maximum-scale=1,minimum-scale=1,user-scalable=no">

    <link rel="stylesheet" href="./css/index.css" />
</head>

<body>
    <h1>JavaScript 异常</h1>
    <h2>参考链接</h2>
    <ul>
        <li><a target="_blank"
                href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Control_flow_and_error_handling#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E8%AF%AD%E5%8F%A5">JavaScript
                异常处理语句</a></li>
    </ul>

    <h2>摘要</h2>
    <p>JavaScript可以抛出任意对象的异常, 并且用 try...catch 语句捕获处理它。</p>

    <h3>throw 语句</h3>
    <p>throw expression;</p>
    <p>使用throw语句抛出一个异常。当你抛出异常，你规定一个含有值的表达式要被抛出</p>

    <pre>
throw "Error2";   // String type
throw 42;         // Number type
throw true;       // Boolean type
throw {toString: function() { return "I'm an object!"; } };
    </pre>

    <h3>try...catch 语句</h3>
    <p>try...catch 语句标记一块待尝试的语句，并规定一个以上的响应应该有一个异常被抛出。如果我们抛出一个异常，try...catch语句就捕获它。</p>
    <p>finally块包含了在try和catch块完成后、下面接着try...catch的语句之前执行的语句。finally块无论是否抛出异常都会执行。如果抛出了一个异常，就算没有异常处理，finally块里的语句也会执行。</p>

    <pre>
function f() {
    try {
        console.log(0);
        throw "bogus";
    } catch(e) {
        console.log(1);
        return true; // this return statement is suspended
                    // until finally block has completed
        console.log(2); // not reachable
    } finally {
        console.log(3);
        return false; // overwrites the previous "return"
        console.log(4); // not reachable
    }
    // "return false" is executed now  
    console.log(5); // not reachable
}
f(); // console 0, 1, 3; returns false
    </pre>
</body>

</html>